package com.mpower.directpay;

import com.slayer.model.PaypalBucket;
import com.slayer.service.PaypalBucketLocalServiceUtil;
import com.paypal.sdk.core.nvp.NVPDecoder;
import com.paypal.sdk.core.nvp.NVPEncoder;
import com.paypal.sdk.profiles.APIProfile;
import com.paypal.sdk.profiles.ProfileFactory;
import com.paypal.sdk.services.NVPCallerServices;

public class DoDirectPayment 
{ 
	private NVPCallerServices caller = null;
 
	public String DoDirectPaymentCode(String paymentAction,String amount,String cardType,
								String acct,String expdate,String cvv2, String firstName,
								String lastName, String street, String city, String state, 
								String zip, String countryCode)
	{
		NVPEncoder encoder = new NVPEncoder();
		NVPDecoder decoder = new NVPDecoder(); 
		try
		{ 
        	caller = new NVPCallerServices();
		APIProfile profile = ProfileFactory.createSignatureAPIProfile();		
		PaypalBucket bucket=PaypalBucketLocalServiceUtil.getPaypalBucket(1);
		
			profile.setAPIUsername(bucket.getApiUserName().trim());
			profile.setAPIPassword(bucket.getApiPassword().trim());
			profile.setSignature(bucket.getApiSignature().trim());
/*
			profile.setAPIUsername(bucket.getApiUserName());
			profile.setAPIPassword(bucket.getApiPassword());
			profile.setSignature(bucket.getApiSignature());
*/
			profile.setEnvironment("sandbox");
			profile.setSubject("");
	   
	        caller.setAPIProfile(profile);
			encoder.add("VERSION", "51.0");
			encoder.add("METHOD","DoDirectPayment"); 
			encoder.add("PAYMENTACTION",paymentAction);
			encoder.add("AMT",amount);
			encoder.add("CREDITCARDTYPE",cardType);		
			encoder.add("ACCT",acct);						
			encoder.add("EXPDATE",expdate);
			encoder.add("CVV2",cvv2);
			encoder.add("FIRSTNAME",firstName);
			encoder.add("LASTNAME",lastName);										
			encoder.add("STREET",street);
			encoder.add("CITY",city);	
			encoder.add("STATE",state);			
			encoder.add("ZIP",zip);	
			encoder.add("COUNTRYCODE",countryCode);

			String NVPRequest = encoder.encode();
			String NVPResponse =(String) caller.call(NVPRequest);
			decoder.decode(NVPResponse); 
		} catch(Exception ex)
		{
			ex.printStackTrace();
		}
		return decoder.get("ACK");
	}	
}